package com.dycx.system.converter;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dycx.common.enums.PlatformTypeEnum;
import com.dycx.common.vendor.oss.AliyunOss;
import com.dycx.system.dto.AppVersionDTO;
import com.dycx.system.entity.AppVersionDO;
import com.dycx.system.enums.AppEnum;
import com.dycx.system.vo.AppVersionVO;
import lombok.Data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * (AppVersion)实体转换类
 *
 * @author wuzongbing
 * @since 2024-02-22 15:25:36
 */


@Data
public class AppVersionConverter implements Serializable {
    public static AppVersionDO dtoToEntity(AppVersionDTO dto) {
        if (dto == null) return null;
        AppVersionDO entity = BeanUtil.toBean(dto, AppVersionDO.class);
        entity.setApkUrl(AliyunOss.delDomain(entity.getApkUrl()));
        return entity;
    }

    public static AppVersionVO entityToVo(AppVersionDO entity) {
        if (entity == null) return null;
        AppVersionVO vo = BeanUtil.toBean(entity, AppVersionVO.class);
        vo.setAppName(AppEnum.converter(vo.getAppId()));
        vo.setApkUrl(AliyunOss.addDomain(vo.getApkUrl()));
        return vo;
    }

    public static List<AppVersionVO> entityToVoList(List<AppVersionDO> entityList) {
        List<AppVersionVO> voList = new ArrayList<>();
        for (AppVersionDO entity : entityList) {
            voList.add(entityToVo(entity));
        }
        return voList;
    }

    public static Page<AppVersionVO> entityToVoPage(Page<AppVersionDO> page) {

        List<AppVersionVO> newRecords = new ArrayList<>();

        List<AppVersionDO> records = page.getRecords();
        for (AppVersionDO entity : records) {
            newRecords.add(entityToVo(entity));
        }

        Page<AppVersionVO> voPage = new Page<>();
        voPage.setRecords(newRecords);
        voPage.setPages(page.getPages());
        voPage.setTotal(page.getTotal());
        voPage.setSize(page.getSize());
        voPage.setCurrent(page.getCurrent());

        return voPage;
    }

}
